//
//  YFStatusOriginalView.swift
//  微博
//
//  Created by 余仿 on 16/4/12.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage
/// 这个类用于描述原创部分的微博界面


/*CUICatalog: Invalid asset name supplied:
设置图片的时候,图片名为空"",不是为nil,而是为空串
*/

class YFStatusOriginalView: UIView {

    var bottomCons:Constraint?

    var statusViewModel:YFStatusViewModel?{

        didSet{
            //给子控件赋值
            //此处必须每次滑动重新赋值,才不会造成单元格重用
            iconView.sd_setImageWithURL(statusViewModel?.headImageURL)

            nameLabel.text = statusViewModel?.statusList?.user?.name

            mbRankImageView.image = statusViewModel?.mbImage

            verifiedImageView.image = statusViewModel?.verifiedImage

            contentLabel.text = statusViewModel?.statusList?.text

            sourceLabel.text = statusViewModel?.source

            timeLabel.text = statusViewModel?.time

            updateBottomCons()
        }
    }

    func updateBottomCons(){

        self.bottomCons?.uninstall()

        let pic_urls = statusViewModel?.statusList?.pic_urls

        if pic_urls != nil &&  pic_urls?.count > 0{
            //说明有图片
            self.snp_makeConstraints(closure: { (make) -> Void in
                self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
            })

            pictureView.hidden = false

            pictureView.picUrls = pic_urls

        }else{
            //说明没图片
            self.snp_makeConstraints(closure: { (make) -> Void in
                self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(statusCellMargin).constraint
            })
            pictureView.hidden = true

            pictureView.picUrls = nil

        }

    }

    /// 分隔条
    private lazy var sepView:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.redColor()
        return view
    }()

    /// MARK:用户头像
    private lazy var iconView:UIImageView = { let icon = UIImageView() ; icon.cornerRadius = 25 ; return icon}()

    /// MARK:用户名
    private lazy var nameLabel = UILabel(title: "", fontSize: 17, fontColor: UIColor.darkGrayColor())

    /// MARK: 用户认证类型(企业,个人....)
    private  lazy var verifiedImageView = UIImageView(image: UIImage(named: "www"))

    /// MARK:用户等级(用于vip级别)
    private lazy var mbRankImageView = UIImageView(image: UIImage(named: "www"))

    /// MARK: 微博创建时间
    private lazy var timeLabel = UILabel(title: "00:20", fontSize: 12, fontColor: UIColor.orangeColor())

    /// MARK: 微博来源
    private lazy var sourceLabel = UILabel(title: "小于", fontSize: 12, fontColor: UIColor.grayColor())

    /// MARK: 微博信息内容
    private lazy var contentLabel = UILabel(title: "下雨啦,收衣服来", fontSize: 17, fontColor: UIColor.darkTextColor(),margin: statusCellMargin)

    /// MARK:图片
    private lazy var pictureView:YFPictureView = YFPictureView()

    // MARK:添加到父视图上,并布局
    override init(frame: CGRect) {

        super.init(frame: frame)

        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(verifiedImageView)
        addSubview(mbRankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)

        setupUI()

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func setupUI(){

        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(statusCellMargin)
        }

        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(self).offset(statusCellMargin)
            make.width.height.equalTo(50)
        }

        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(statusCellMargin)
            make.top.equalTo(iconView.snp_top)
        }

        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }

        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.bottom.equalTo(iconView)
        }

        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.bottom.equalTo(iconView.snp_bottom)
        }

        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
        }

        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_left)
            make.top.equalTo(iconView.snp_bottom).offset(statusCellMargin)
        }

        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //可以设置父视图相对子视图的布局,用于自动计算高度
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
            
        }
    }
}
